"""
def demo(a,*args,b=1,**kwargs):
    print(a, 'a')
    print(args)
    print(b, 'b')
    print(kwargs)
    
demo(100,5,6,7,8,9,b=77,c=777)
demo(a=33)

"""

# 解包*: 解包可以解包元组, 也可以解包数组
"""
def greet(name,age,city):
    print(name)
    print(age)
    print(city)

greet(*['Alice', 18, 'beijing'])
greet(*('Bob', 19, 'shanghai'))

"""

# 解包**: 解包可以解字典
"""
def greet(name, age, city):
    print(name)
    print(age)
    print(city)

greet(**{'name': 'zs', 'age': '18', 'city': 'chongqing'})

"""

# 解包混合使用
"""
def demo(a,b,c,d,e):
    print(a)
    print(b)
    print(c)
    print(d)
    print(e)

demo('1a',*['b2','c3'],**{'d': 'd4','e': 'e5'})

"""

"""
案例: 计算总分
def calculate_sujects(name, **scores):
    total_info = {
        'total': 0,
        'name': name,
        **scores
    }
    for score in scores.values():
        # total_info['total'] = total_info['total'] + score
        total_info['total'] += score

    print(total_info)

calculate_sujects('Alice', math=100, english=77)
"""

"""
案例: 回调函数

def operator_callback(numbers, callback):
    return [callback(x) for x in numbers]

def doubble(n):
    return n * 2

def square(n):
    return n ** 2

numbers = [1,2,3,4,5]
r1 = operator_callback(numbers, doubble)
r2 = operator_callback(numbers, square)
print(r1)
print(r2)
def demo():
    pass
demo.a = 1
print(demo.a)
print(demo)

"""

"""
# 装饰器

def wrapper_fn(fn):
    def wrapper(*args, **kwargs):
        print('调用前')
        fn(*args, **kwargs)
        print('调用后')
    return wrapper

@wrapper_fn
def demo_fn(a,b,c,d):
    print(a + b)
    print(c * d)
    print('执行结束')

demo_fn(1,2,c=7,d=7)
# demo_fn(1,2) 报错

"""

"""
匿名函数: lambda
dobule = lambda x: x *2
dr = dobule(2)
print(dr)
# 按成绩排序
score_list = [
    {'name': 'Alice', 'score': 100},
    {'name': 'Bob', 'score': 78},
    {'name': 'Tim', 'score': 88}
]

score_list2 = [
    {'name': 'Alice', 'score': '100'},
    {'name': 'Bob', 'score': '78'},
    {'name': 'Tim', 'score': '88'}
]

sr = sorted(score_list, key=lambda x: x['score'], reverse=True)
sr2 = sorted(score_list2, key=lambda x: x['score'])
print(sr2)
print(sr)

numbers = [1,2,3,4]
mr = map(lambda x: x * 2,numbers)
lmr = list(mr)
lmr2 = list(mr)
print(lmr)
print(lmr2)
print(mr)

"""














